package androidx.lifecycle;

import android.os.Bundle;
import androidx.lifecycle.e;
import androidx.savedstate.SavedStateRegistry;
import androidx.savedstate.b;

final class SavedStateHandleController implements f {

    /* renamed from: a  reason: collision with root package name */
    private final String f726a;

    /* renamed from: b  reason: collision with root package name */
    private boolean f727b = false;

    /* renamed from: c  reason: collision with root package name */
    private final q f728c;

    static final class a implements SavedStateRegistry.a {
        a() {
        }

        public void a(b bVar) {
            if (bVar instanceof v) {
                u viewModelStore = ((v) bVar).getViewModelStore();
                SavedStateRegistry savedStateRegistry = bVar.getSavedStateRegistry();
                for (String b2 : viewModelStore.c()) {
                    SavedStateHandleController.h(viewModelStore.b(b2), savedStateRegistry, bVar.getLifecycle());
                }
                if (!viewModelStore.c().isEmpty()) {
                    savedStateRegistry.e(a.class);
                    return;
                }
                return;
            }
            throw new IllegalStateException("Internal error: OnRecreation should be registered only on componentsthat implement ViewModelStoreOwner");
        }
    }

    SavedStateHandleController(String str, q qVar) {
        this.f726a = str;
        this.f728c = qVar;
    }

    static void h(s sVar, SavedStateRegistry savedStateRegistry, e eVar) {
        SavedStateHandleController savedStateHandleController = (SavedStateHandleController) sVar.c("androidx.lifecycle.savedstate.vm.tag");
        if (savedStateHandleController != null && !savedStateHandleController.l()) {
            savedStateHandleController.i(savedStateRegistry, eVar);
            m(savedStateRegistry, eVar);
        }
    }

    static SavedStateHandleController j(SavedStateRegistry savedStateRegistry, e eVar, String str, Bundle bundle) {
        SavedStateHandleController savedStateHandleController = new SavedStateHandleController(str, q.a(savedStateRegistry.a(str), bundle));
        savedStateHandleController.i(savedStateRegistry, eVar);
        m(savedStateRegistry, eVar);
        return savedStateHandleController;
    }

    private static void m(final SavedStateRegistry savedStateRegistry, final e eVar) {
        e.b b2 = eVar.b();
        if (b2 == e.b.INITIALIZED || b2.a(e.b.STARTED)) {
            savedStateRegistry.e(a.class);
        } else {
            eVar.a(new f() {
                public void d(h hVar, e.a aVar) {
                    if (aVar == e.a.ON_START) {
                        eVar.c(this);
                        savedStateRegistry.e(a.class);
                    }
                }
            });
        }
    }

    public void d(h hVar, e.a aVar) {
        if (aVar == e.a.ON_DESTROY) {
            this.f727b = false;
            hVar.getLifecycle().c(this);
        }
    }

    /* access modifiers changed from: package-private */
    public void i(SavedStateRegistry savedStateRegistry, e eVar) {
        if (!this.f727b) {
            this.f727b = true;
            eVar.a(this);
            savedStateRegistry.d(this.f726a, this.f728c.b());
            return;
        }
        throw new IllegalStateException("Already attached to lifecycleOwner");
    }

    /* access modifiers changed from: package-private */
    public q k() {
        return this.f728c;
    }

    /* access modifiers changed from: package-private */
    public boolean l() {
        return this.f727b;
    }
}
